﻿using System;
using System.IO;
using System.Xml;
using System.Security.Cryptography;
using System.Text;
using System.IO;

namespace DBConnection
{
    public static class FileUtility
    {
        public static bool ReadConfig(String filePath, out String source, out String user, 
            out String password, out String db, out String securityType)
        {
            if (!File.Exists(filePath))
            {
                source = "somehost";
                user = "user";
                password = "password";
                db = "database";
                securityType = "true";
                return false;
            }

            XmlDocument configFile = new XmlDocument();
            configFile.Load(filePath);

            source = configFile.GetElementsByTagName("SERVER")[0].InnerText;
            user = configFile.GetElementsByTagName("USERNAME")[0].InnerText;
            password = configFile.GetElementsByTagName("PASSWORD")[0].InnerText;
            db = configFile.GetElementsByTagName("DBNAME")[0].InnerText;
            securityType = configFile.GetElementsByTagName("SECURITY")[0].InnerText;
            return true;
        }

        public static bool ReadConfig(String filePath, out String source, out String user,
            out String password, out String db, out String securityType,
            out String connTimeout, out String commandTimeout)
        {
            if (!File.Exists(filePath))
            {
                source = "somehost";
                user = "user";
                password = "password";
                db = "database";
                securityType = "true";
                connTimeout = "30";
                commandTimeout = "30";
                return false;
            }

            XmlDocument configFile = new XmlDocument();
            configFile.Load(filePath);

            source = configFile.GetElementsByTagName("SERVER")[0].InnerText;
            user = configFile.GetElementsByTagName("USERNAME")[0].InnerText;
            password = configFile.GetElementsByTagName("PASSWORD")[0].InnerText;
            db = configFile.GetElementsByTagName("DBNAME")[0].InnerText;
            securityType = configFile.GetElementsByTagName("SECURITY")[0].InnerText;
            connTimeout = configFile.GetElementsByTagName("CONNTIMEOUT")[0].InnerText;
            commandTimeout = configFile.GetElementsByTagName("COMTIMEOUT")[0].InnerText;
            return true;
        }

        public static bool UpdateConfig(string filepath, string source, string user,
            string password, string db, string securityType, string connTimeout, string commandTimeout)
        {
            if (!File.Exists(filepath))
                return false;

            XmlDocument configFile = new XmlDocument();
            configFile.Load(filepath);

            configFile.GetElementsByTagName("SERVER")[0].InnerText = source;
            configFile.GetElementsByTagName("USERNAME")[0].InnerText = user;
            configFile.GetElementsByTagName("PASSWORD")[0].InnerText = password;
            configFile.GetElementsByTagName("DBNAME")[0].InnerText = db;
            configFile.GetElementsByTagName("SECURITY")[0].InnerText = securityType;
            configFile.GetElementsByTagName("CONNTIMEOUT")[0].InnerText = connTimeout;
            configFile.GetElementsByTagName("COMTIMEOUT")[0].InnerText = commandTimeout;

            configFile.Save(filepath);
            return true;
        }

        public static bool WriteLine(String filepath, String message)
        {
            StreamWriter targetFile = new StreamWriter(filepath, true, System.Text.Encoding.UTF8);
            targetFile.WriteLine(message);
            targetFile.Close();
            return true;
        }

        public static bool Write(String filepath, String message)
        {
            StreamWriter targetFile = new StreamWriter(filepath, true, System.Text.Encoding.UTF8);
            targetFile.Write(message);
            targetFile.Close();
            return true;
        }

        public static bool WriteXml(string filepath, string rootName, string[] rootAttributesNames, string[] rootAttributesValues, string message)
        {
            XmlDocument targetFile = new XmlDocument();
            XmlElement root;
            if (File.Exists(filepath))
            {
                try
                {
                    targetFile.Load(filepath);
                    root = targetFile.DocumentElement;
                }
                catch
                {
                    targetFile.CreateXmlDeclaration("1.0", "UTF-8", String.Empty);
                    root = targetFile.CreateElement(rootName);
                    if (rootAttributesNames != null
                        && rootAttributesValues != null
                        && rootAttributesNames.Length == rootAttributesValues.Length)
                    {
                        for (int i = 0; i < rootAttributesNames.Length; i++)
                            root.SetAttribute(rootAttributesNames[i], rootAttributesValues[i]);
                    }
                    targetFile.AppendChild(root);
                }
            }
            else
            {
                if (!Directory.Exists("bugs"))
                    Directory.CreateDirectory("bugs");
                StreamWriter writer = new StreamWriter(filepath, false, System.Text.Encoding.UTF8);
                writer.Close();

                targetFile.CreateXmlDeclaration("1.0", "UTF-8", String.Empty);
                root = targetFile.CreateElement(rootName);
                if (rootAttributesNames != null
                        && rootAttributesValues != null
                        && rootAttributesNames.Length == rootAttributesValues.Length)
                {
                    for (int i = 0; i < rootAttributesNames.Length; i++)
                        root.SetAttribute(rootAttributesNames[i], rootAttributesValues[i]);
                }
                targetFile.AppendChild(root);
            }

            root.InnerXml += message;
            targetFile.Save(filepath);
            return true;
        }

        public static string GetAttributeXml(string filePath, string nodeName, string attributeName)
        {
            if (!File.Exists(filePath))
                return null;

            XmlDocument targetFile = new XmlDocument();
            try
            {
                targetFile.Load(filePath);
            }
            catch
            {
                return null;
            }
            XmlNodeList nodes = targetFile.GetElementsByTagName(nodeName);
            if (nodes.Count == 0)
                return null;
            return nodes[0].Attributes[attributeName].Value;
        }

        public static bool SetAttributeXml(string filePath, string nodeName, string attributeName, string attributeValue)
        {
            if (!File.Exists(filePath))
                return false;

            XmlDocument targetFile = new XmlDocument();
            try
            {
                targetFile.Load(filePath);
            }
            catch
            {
                return false;
            }
            XmlNodeList nodes = targetFile.GetElementsByTagName(nodeName);
            if (nodes.Count == 0)
                return false;
            ((XmlElement)nodes[0]).SetAttribute(attributeName, attributeValue);
            targetFile.Save(filePath);
            return true;
        }

        public static string ComputeHash(string plainText)
        {
            SHA1 code = new SHA1CryptoServiceProvider();
            return Convert.ToBase64String(code.ComputeHash(ASCIIEncoding.ASCII.GetBytes(plainText)));
        }
    }
}
